// service/distribution/distribution.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据 app.globalData.user
   */
  data: {
    couponShow: false,
    closeOnClickOverlay: true,
    info:{},
    userInfo:{},
    qrBgImgUrl: 'https://xckj-auto-file-res.oss-cn-shenzhen.aliyuncs.com/uploads/qrcode-bg.png',
    qrcodeUrl: '',
  },


  link(){
    wx.setClipboardData({
      data: 'asdasd',
      success (res) {
        util.toast('已复制')
      }
    })
  },
  poster(){
    wx.showLoading({
      title: '生成中...',
    })
    const query = wx.createSelectorQuery()
    query.select('#myCanvas')
      .fields({ 
        node: true, 
        size: true 
      }).exec(async (res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')
        // 设置canvas尺寸
        canvas.width = 598;
        canvas.height = 1244;
        // 绘制网络图片
        const bgImg = canvas.createImage()
        bgImg.src = this.data.qrBgImgUrl;
        bgImg.onload = () => {
          ctx.drawImage(bgImg, 0, 0, 598, 1244); 

          // 绘制小程序码
          const qrcodeImg = canvas.createImage()
          qrcodeImg.src = util.cdn(this.data.qrcodeUrl);
          qrcodeImg.onload = () => {
            ctx.drawImage(qrcodeImg, 100, 776, 390, 390);
            
            // 保存canvas为图片
            wx.canvasToTempFilePath({
              canvas: canvas, 
              success: res => {
                // 导出的图片路径存储在res.tempFilePath中，‌可以进行保存或分享操作
                wx.hideLoading()
                this.saveImg(res.tempFilePath)
              }
            });
          }
        }
        
      })
  },
  weChat(){

  },
  openPopup(){
    this.getBuildQrcode()
    this.setData({
      couponShow: true
    })
  },
  onClose(){
    this.setData({
      couponShow:false
    })
  },
  viewDetaiTap(){
    const obj = {
      delay_money: this.data.info.data?.delay_money || 0,
      money: this.data.userInfo?.money || 0,
      total_income: this.data.info.data?.total_income || 0
    }
    wx.setStorageSync('brokerage', JSON.stringify(obj))
    util.skip('/service/distribution/brokerage/brokerage')
  },
  shareRecordTap(e){
    const url = e.currentTarget.dataset.url
    util.skip(url)
  },
  getInfo(){
    http.post('/addons/shopro/commission.agent/info').then(res => {
      this.setData({
        info: res.data
      })
    })
  },
  getUserInfo(){
    http.post('/addons/shopro/user').then(res => {
      this.setData({
        userInfo: res.data
      })
    })
  },
  getBuildQrcode(){
    http.post('/addons/shopro/share/buildQrcode').then(res => {
      this.setData({
        qrcodeUrl: '/' + res.data.image_url
      })
    })
  },
  getImageInfo(url){
    return new Promise((resolve,reject) => {
      wx.getImageInfo({
        src: url, // 背景图片的网络地址
        success: function(res) {
          resolve(res)
        }
      });
    })
  },
  saveImg(tempFilePath){
    wx.saveImageToPhotosAlbum({
      filePath: tempFilePath,
      success(res) {
        util.toast('保存成功')
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(option) {
    let data = option.target.dataset.id; //取到传入的参数
    //来自页面按钮内的分享
    if(option.from === 'button'){

    }
    return {
      title: '分享',
      path: `pages/index/index?parent_user_id=${app.globalData.user.id || 0}`,
      imageUrl: '',
      success: () => {
          console.log('分享成功');
      },
      fail: () => {
          console.log('分享失败');
      },
    };
  }
})